package com.hqyj.system.controller;

import com.hqyj.system.model.Role;
import com.hqyj.system.model.User;
import com.hqyj.system.model.UserRole;
import frame.system.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author cz
 * 2021/9/2 10:57
 */
@Controller
@RequestMapping("/roleController")
public class RoleController extends BaseController {

    @RequestMapping("/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) {

        List<Role> role_dbList = roleService.queryAllRole();
        System.out.println(role_dbList);
        model.addAttribute("role_dbList", role_dbList);
        //2.跳转页面;
        return "view/system/role/roleList";
    }

    @RequestMapping("/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Role role) {

        int n = roleService.addRoleByRole(role);

        List<Role> role_dbList = roleService.queryAllRole();
        System.out.println(role_dbList);
        model.addAttribute("role_dbList", role_dbList);
        //2.跳转页面;
        return "view/system/role/roleList";
    }

    @RequestMapping("/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Role role, String idStr) {
        int id = Integer.parseInt(idStr);
        int n = roleService.deleteRoleByidStr(id);

        List<Role> role_dbList = roleService.queryAllRole();
        System.out.println(role_dbList);
        model.addAttribute("role_dbList", role_dbList);
        //2.跳转页面;
        return "view/system/role/roleList";
    }

    @RequestMapping("/updateUI.do")
    public String updateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model,String idStr) {
        int roleId = Integer.parseInt(idStr);
        Role role_db = roleService.selectRoleByRoleId(roleId);

        model.addAttribute("role", role_db);
        //2.跳转页面;
        return "view/system/role/updateRole";
    }

    @RequestMapping("/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Role role, String roleIdStr) {
        role.setRoleId(Integer.parseInt(roleIdStr));
        int n = roleService.updateRoleByRoleId(role);

        List<Role> role_dbList = roleService.queryAllRole();
        System.out.println(role_dbList);
        model.addAttribute("role_dbList", role_dbList);
        //2.跳转页面;
        return "view/system/role/roleList";
    }

    @RequestMapping("/FPRoleUI.do")
    public String FPRoleUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, Role role, String userIdStr) {
        int userId = Integer.parseInt(userIdStr);

        //分配用户数据
        User user_db = userService.queryUserById(userId);
        model.addAttribute("user_db", user_db);

        //分配角色数据
        List<Role> role_dbList = roleService.queryAllRole();
        model.addAttribute("role_dbList", role_dbList);

        //选中数据
        Role Role_db_XZ = roleService.queryRoleByUserId(userId);
        model.addAttribute("Role_db_XZ", Role_db_XZ);
        //2.跳转页面;
        return "view/system/role/FPRoleUI";
    }

    @RequestMapping("/FPRole.do")
    public String FPRole(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, UserRole userRole, Role role, String userIdStr, String roleIdSTR) {
        int userId = Integer.parseInt(userIdStr);
        int roleId = Integer.parseInt(roleIdSTR);

        //设置数据
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);
        //先删除，通过userId
        int m = userRoleService.deleteUserRoleByUserId(userId);
        //调用业务
        int n = userRoleService.addUserRoleByUserRole(userRole);

        //分配用户数据
        User user_db = userService.queryUserById(userId);
        model.addAttribute("user_db", user_db);

        //分配角色数据
        List<Role> role_dbList = roleService.queryAllRole();
        model.addAttribute("role_dbList", role_dbList);

        //友情提示
        model.addAttribute("message", "分配角色成功，继续分配吗？");

        //选中数据
        Role Role_db_XZ = roleService.queryRoleByUserId(userId);
        model.addAttribute("Role_db_XZ", Role_db_XZ);

        //2.跳转页面;
        return "view/system/role/FPRoleUI";
    }
}
